import * as opreate from 'api/getData'

/**
* @desc: This is Note
* 对象型数组去重函数
* @author: WJ
* @date: 2018/10/23 09:35:20
*/
const setDataSolve = (list) => {
  let temSet = {}
  let listTem = list.reduce((item, next) => {
    temSet[next.productCode] ? '' : temSet[next.productCode] = true && item.push(next);
    return item;
  }, [])
  return listTem
}
/**
 * @desc: This is Note
 * 市场数据----状态管理
 * @author: WJ
 * @date: 2018/10/09 21:45:08
 **/
export const marketSaleParam = {
  state: {
    commentCountMessage: {},
    discussCommentInit: {},
    commentCount: {},
    productCode: '',
    discussCareRank: [],
    pushOptions: [{
      label: '手机信息推送',
      value: 'textMessage'
    }, {
      label: '邮件推送',
      value: 'emailMessage'
    }, {
      label: 'APP应用推送',
      value: 'appMessage'
    }],
    prodCtrolStateInfo: {},
    renderOrganization: {},
    prodTargetPopulation: {},
    prodCtrolInfoMarketing: {},
    compareItemsProd: [],
    compareUnLineMarketing: [],
    compareOnLineMarketing: [],
    compareProdCodeListByCate: [],
    compareBrandListByCate: [],
    compareProdAndBrandListByCate: [],
    compareAllCodeByCate: [],
    yearTime:'',
    mineSetRegion:'',
    mineSetProvine: '',
    mineSetCity: ''
  },
  getters: {
    commentCountMessage: state => state.commentCountMessage,
    discussCommentInit: state => state.discussCommentInit,
    commentCount: state => state.commentCount,
    productCode: state => state.productCode,
    discussCareRank: state => state.discussCareRank,
    pushOptions: state => state.pushOptions,
    prodCtrolStateInfo: state => state.prodCtrolStateInfo,
    renderOrganization: state => state.renderOrganization,
    prodTargetPopulation: state => state.prodTargetPopulation,
    prodCtrolInfoMarketing: state => state.prodCtrolInfoMarketing,
    compareItemsProd: state => state.compareItemsProd,
    compareUnLineMarketing: state => state.compareUnLineMarketing,
    compareOnLineMarketing: state => state.compareOnLineMarketing,
    compareProdCodeListByCate: state => state.compareProdCodeListByCate,
    compareBrandListByCate: state => state.compareBrandListByCate,
    compareProdAndBrandListByCate: state => state.compareProdAndBrandListByCate,
    compareAllCodeByCate: state => state.compareAllCodeByCate,
    yearTime:state => state.yearTime
  },
  mutations: {
    updateCustomerConcern(state, data) {
      let { currentPage, totalRecord, list } = data || {}
      state.commentCountMessage = { currentPage: currentPage || 0, totalRecord: totalRecord || 0 }
      state.discussCommentInit = list ? list.map(v => {
        let item = v
        item.sentimentType = item.sentimentType.substr(0,1)
        item.esntimentPicture = item.esntimentPicture.split(',')
        item.focuxPoint = item.focuxPoint.join(',')
        return item
      }) : []
    },
    updateCommentCount(state, data){
      state.commentCount = data
    },
    updateProductCode(state, data){
      state.productCode = data
    },
    updateDiscussCareRank(state, data){
      state.discussCareRank = data
    },
    updateProdCtrolStateInfo(state, data){
      state.prodCtrolStateInfo = data
    },
    updateRenderOrganization(state, data){
      state.renderOrganization = data || {}
    },
    updateProdTargetPopulation(state, data){
      state.prodTargetPopulation = data
    },
    updateProdCtrolInfoMarketing(state, data){
      state.prodCtrolInfoMarketing = data
    },
    updateCompareItemsProd(state, data){
      let { payload, flag } = data
      if(flag){
        state.compareItemsProd = payload
      }else {
        let temItemsProd = state.compareItemsProd.concat(payload)
        state.compareItemsProd = setDataSolve(temItemsProd)
      }
    },
    spliceCompareItemsProd(state, index){
      state.compareItemsProd.splice(index,1)
    },
    updateCompareUnLineMarketing(state, data){
      let { payload, flag } = data
      if(flag){
        state.compareUnLineMarketing = payload
      }else {
        let temItemsProd = state.compareUnLineMarketing.concat(payload)
        state.compareUnLineMarketing = temItemsProd
      }
    },
    spliceCompareUnLineMarketing(state, index){
      state.compareUnLineMarketing.splice(index,1)
    },
    updateCompareOnLineMarketing(state, data){
      let { payload, flag } = data
      if(flag){
        state.compareOnLineMarketing = payload
      }else {
        let temItemsProd = state.compareOnLineMarketing.concat(payload)
        state.compareOnLineMarketing = temItemsProd
      }
    },
    spliceCompareOnLineMarketing(state, index){
      state.compareOnLineMarketing.splice(index,1)
    },
    updateCompareCodeByCate(state, data){
      if(data.length > 0){
        state.compareProdCodeListByCate = data.find(v => v.productId == "all") ? data : [{productId: "all", productCode: "全部"}, ...data]
      }else {
        state.compareProdCodeListByCate = []
      }
    },
    updateCompareAllCodeByCate(state, data){
      if(data.length > 0){
        state.compareAllCodeByCate = [...new Set([{productId: "all", productCode: "全部"}, ...data])]
      }else {
        state.compareAllCodeByCate = []
      }
    },
    updateCompareBrandListByCate(state, data){
      state.compareBrandListByCate = [...new Set([{ brandId: "all", brandName: "全部" }, ...data])]
    },
    updateCompProdAndBrandByCate(state, data){
      state.compareProdAndBrandListByCate = data
    },
    yearTime(state, data){
      state.yearTime = data
    }
  },
  actions: {
    obtainCustomerConcern({commit},params){
      opreate.getCommentCountMessage(params).then(res => {
        commit('updateCustomerConcern',res.data || {})
      })
    },
    obtainCommentCount({commit},params){
      opreate.getCommentCount(params).then(res => {
        commit('updateCommentCount',res.data)
      })
    },
    obtainDiscussCareRank({commit},params){
      opreate.careRank(params).then(res => {
        commit('updateDiscussCareRank',res.data)
      })
    },
    obtainProdTargetPopulation({commit},params){
      opreate.getProductTargetPopulation(params).then(res => {
        commit('updateProdTargetPopulation',res.data || {})
      })
    },
    obtainRenderOrganization({commit}){
      opreate.getOrganizationTree().then(res => {
        commit('updateRenderOrganization',res.data)
      })
    },
    obtainCompCodeAndBrandByCate({commit},params){
      opreate.getAnalysisCode(params).then(res => {
        const compareAllCodeByCate = [].concat.apply([],res.data.map(v => v.productCodeList))
        commit('updateCompProdAndBrandByCate',res.data)
        commit('updateCompareBrandListByCate',res.data)
        commit('updateCompareCodeByCate',compareAllCodeByCate)
        commit('updateCompareAllCodeByCate',compareAllCodeByCate)
      })
    }
  }
}
